<?php
function developmentpage_info($context) {
	if ($context) {
		switch ($context) {
			case "modinfo":
				$M = array();
					$config = array();
					$config['title'] = "Developers Test Page";
					$config['modref'] = "developmentpage";
					$config['icon'] = null;//optional
						$includes = array();
							$head = array();
								$head['js']['local'] = array();
								$head['js']['local'][] = "<script>alert('Script Head Include');</script>\n";
								$head['js']['external'] = array();
								$head['js']['external'][] = "<script src='file.js'></script>\n";

								$head['css']['local'] = array();
									$head['css']['local'][] = "<style type='text/css'>.body {font-size:10px;}</style>\n";
								$head['css']['external'] = array();
									$head['css']['external'][] = "<link type=\"text/css\" rel=\"stylesheet\" media=\"all\" href=\"mycssfile.css\" />\n";
						$includes['head'] = $head;
							$page['body'] = array();
								$page['body']['js']['local'] = array();
								
								$page['body']['js']['external'] = array();
						$incluldes['body'] = $page['body'];
					$config['includes'] = $includes;
					//$config[''] = null;
				$M['config'] = $config;
				return $M;
			break;
			case "help":
				$page = array();
				$page['title'] = "Development Page: Help";
				$page['body'] = "<p>help content here</p>\n";
			break;
			case "about":
				$page = array();
				$page['title'] = "Development Page: About";
				$page['body'] = "<p>Safe-Harbour Development Page module designed by mike@ox-code.org</p>\n";
			break;
		}//switch context
	}
}
function developmentpage_title($Q,$dblayer) {

	$titleImageSrc = "";
	if (isset($Q['icons']['map']['large']['actions']['code-class'])) {
		$titleImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['large']['actions']['code-class'];
	} else {
		$titleImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	}
	$title = "<h1><img valign='middle' src='{$titleImageSrc}' />&nbsp;Developer Tools</h1>\n";
	
	return $title;
}
function developmentpage_schema_check($Q,$default,$dbLabel=null) {
if ($dbLabel == null) {
	$dbLabel = "SYSTEM";
}
$page = "";
$sqlupdate = "";//the eventual transaction of SQL statements
	$xmlfile = $Q{'absolute_path'}."/site/modules/developmentpage/schema.xml";
	if (file_exists($xmlfile)) {
		$xml = simplexml_load_file($xmlfile);
		//print_r($xml);
		if ($xml) {
			//$database = $xml->name;
			$database = $default->param($dbLabel,"dbname");
			foreach ($xml->tables->table as $table) {
				//check if each table exists and that it's fields match.
				$page .= "<h3>".$table->name."</h3>\n";
				$sql = sprintf("SELECT * FROM `%s`",$table->name);
				$dbname = $default->param($dbLabel,"dbname");
				$dbconn = $default->param($dbLabel,"dbconn");
				mysql_select_db($dbname, $dbconn);
				$result = @mysql_query($sql,$dbconn);
				if (!$result) {
					$page .= $table->name." does not exist!";
								//$sqlupdate .= "USE {$dbname}\n";
								$sqlupdate .= "CREATE TABLE `{$database}`.`".$table->name."`";
								$primaryKey = "";
								if (count($table->fields->field)) {
									$sqlupdate .= "(\n";
									foreach ($table->fields->field as $field) {
										$null = "";
										if ($field->notNull == "true") {
											$null = " NOT NULL ";
										}
										$autoInc = "";
										if ($field->autoIncrement == "true") {
											$autoInc = " AUTO_INCREMENT ";
										}
										$defaultValue = "";
										if ($field->defaultValue != "") {
											$defaultValue = " DEFAULT ".$field->defaultValue;
										}
										$sqlupdate .= "`".$field->label."` ".$field->type."{$null}{$autoInc}{$defaultValue},\n";
										if ($field->key == "true") {
											$primaryKey = "PRIMARY KEY (`".$field->label."`)\n";
										}
									}//foreach
									$sqlupdate .= $primaryKey;
									$sqlupdate .= ")";
								}//if count 
								$page .= "<p>Resulting SQL Statement:</p>\n";
								$page .= $sqlupdate;
								$dbname = $default->param($dbLabel,"dbname");
								$dbconn = $default->param($dbLabel,"dbconn");
								$upd = new mysqldb;
								$upd->setquery($dbname,$dbconn,sprintf("%s",$sqlupdate),1);
								$page .= "<p class=\"warning\">We posted the sql.</p>\n";
				} else {
					//$page .= "<p>".$table->name." Exists</p>\n";
						$page .= "<blockquote>\n";
						foreach ($table->fields->field as $field) {
							$fsql = sprintf("select %s from %s",$field->label,$table->name);
							$result = mysql_query($fsql);
							if (!$result) {
								$page .= "<p>".$field->label." does not exist</p>\n";
								//create an SQL statement to create the field.
								$sqlupdate = "";
								
							} else {
								//$page .= "<p>".$field->label." exists</p>\n";
							}
						}//foreach
						$page .= "</blockquote>\n";
					//now check fields
				}
				//disposequery($);
			}//foreach
		}//if xml
	}//if file exists
return $page;
}
function developmentpage_task($Q,$dblayer,$providers,$params) {
$page = array();
$page['title'] = "";
$page['body'] = "";
	if (isset($params['task'])) {
		$task = $params['task'];
		$modref = "";
		if (isset($params['modref'])) {
			$modref = $params['modref'];
		}
		switch ($task) {
		default:
			/** Specifiy the default action **/
			$page['title'] .= "safe-harbour Developer Tools";
			$page['body'] .= "<p>These tools allow Developers to look at different aspects of the system they are developing, this module should not be installed or active on Production Server.</p>\n";
			//$page['body'] .= developmentpage_schema_check($Q,$default);
			//$page['body'] .= "<p><a href='".$Q{'base_url'}."/?module={$modref}&task=config-info'>safe-harbour Active Configuration Page</a></p>";
		break;
		/*case "mike-is-cool":
			$page['title'] .= "Development Page Module: Mike is Cool";
			$page['body'] .= "<p>Mike is Cool</p>\n";
		break;*/
		case "sys-vars":
			$modImageSrc = "";
				if (isset($Q['icons']['map']['small']['actions']['code-class'])) {
					$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['actions']['code-class'];
				}
			$page['title'] .= "System Variables";
			if (($modImageSrc) and ($modImageSrc != "")) {
				$image = "<img valign='middle' src='{$modImageSrc}' />&nbsp;";
			}
			$page['body'] .= "<h2>{$image}safe-harbour System Variables</h2>\n";
			$vars = $dblayer->query("SYSTEM",sprintf("select * from variables order by label ASC"));
				if ($vars->num_rows) {
					while ($row = $vars->fetch_object()) {
						$page['body'] .= "<p>".$row->label.":".$row->data."</p>\n";
					}// while ($vars->row = mysql_fetch_assoc($vars->query));
				}
		break;
		case "config-info":
			$modImageSrc = "";
				if (isset($Q['icons']['map']['small']['actions']['code-class'])) {
					$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['actions']['code-class'];
				}
			$page['title'] .= "System Variables";
			if (($modImageSrc) and ($modImageSrc != "")) {
				$image = "<img valign='middle' src='{$modImageSrc}' />&nbsp;";
			}
			$page['title'] .= "safe-harbour Configuration Page";
			$page['body'] .= "<h2>{$image}Active Configuration Page</h2>";
			$page['body'] .= "<p>&nbsp;</p>\n";
			$page['body'] .= "<h3>Current User</h3>\n";
			$page['body'] .= "<ul>\n";
			$user = current_user($dblayer);
				if ($user) {
					$page['body'] .= "<li><strong>Username:</strong> ".$user['username']."</li>\n";
					$page['body'] .= "<li><strong>Real Name:</strong> ".$user['realname']."</li>\n";
					$page['body'] .= "<li><strong>Source:</strong> ".$user['source']."</li>\n";
					$enabled = "Disabled";
					if ($user['enabled']) {
						$enabled = "Enabled";
					}
					$page['body'] .= "<li><strong>Enabled:</strong> {$enabled}</li>\n";
				}
			$page['body'] .= "</ul>\n";
		$page['body'] .= "</p>\n";
		$page['body'] .= "<p>\n";
		$page['body'] .= "<h3>Base Configuration</h3>\n";
			$page['body'] .= "<ul>\n";
				$page['body'] .= "<li><strong>base_url</strong>: ".$Q{'base_url'}."</li>\n";
				$page['body'] .= "<li><strong>absolute_path</strong>: ".$Q{'absolute_path'}."</li>\n";
				$page['body'] .= "<li><strong>media_url</strong>: ".$Q{'media_url'}."</li>\n";
				$page['body'] .= "<li><strong>media_absolute_path</strong>: ".$Q{'media_absolute_path'}."</li>\n";
			$page['body'] .= "</ul>\n";
		$page['body'] .= "</p>\n";
		$page['body'] .= "<p>\n";
			$page['body'] .= "<h3>Configured Authentication Sources</h3>\n";
			$labels = $providers->labels();
			if (count($labels) ) {
				$page['body'] .= "<ul>\n";
				foreach ($labels as $label) {
					$type = $providers->type($label);
					$page['body'] .= "<li>{$label} <em>{$type}</em></li>\n";
				}
				$page['body'] .= "</ul>\n";
			}
		$page['body'] .= "</p>\n";
		$page['body'] .= "<p>\n";
			$page['body'] .= "<h3>Enabled Modules</h3>\n";
			$modules = $dblayer->query("SYSTEM",sprintf("select * from modules where enabled = 1"));
				if ($modules->num_rows) {
					$page['body'] .= "<ul>\n";
						while ($row = $modules->fetch_object()) {
							if ($row->enabled) {
								$title = $row->title;
							} else {
								$title = "<em>".$row->title."</em>\n";
							}
							$page['body'] .= "<li>{$title}</li>\n";
						}// while ($modules->row = mysql_fetch_assoc($modules->query));
					$page['body'] .= "</ul>\n";
				} else {
					$page['body'] .= "<ul><li>There are no modules enabled.</li></ul>\n";
				}
		$page['body'] .= "</p>\n";
		$page['body'] .= "<p>\n";
			$page['body'] .= "<h3>Available Databases</h3>\n";
			$labels = $default->labels();
			if (count($labels)) {
				$page['body'] .= "<ul>\n";
				foreach ($labels as $label) {
					$page['body'] .= "<li>".$label."</li>\n";
				}
				$page['body'] .= "</ul>\n";
			}
		$page['body'] .= "</p>\n";
		break;
		}//switch task
	}
return $page;
}
function developmentpage_cpanel($Q,$dblayer,$params) {
	$modref = "developmentpage";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$modImageSrc = "";
	if (isset($Q['icons']['map']['large']['actions']['code-class'])) {
		$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['large']['actions']['code-class'];
	} else {
		$modImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	}
	$icons = array();
		$icon = array();
			$icon['label'] = "Developer Tools";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Administration";
		$icons[] = $icon;
	return $icons;
}
function developmentpage_local_tasks($Q,$dblayer,$providers,$params) {
	
	$modImageSrc = "";
	if (isset($Q['icons']['map']['small']['actions']['code-class'])) {
		$modImageSrc = $Q['base_url']."/".$Q['icons']['location']."/".$Q['icons']['map']['small']['actions']['code-class'];
	} else {
		$modImageSrc = system_get_var($dblayer,"default_cpanel_icon");
	}
	$modref = "users";
	if (isset($params['modref'])) {
		$modref = $params['modref'];
	}
	$icons = array();
		$icon = array();
			$icon['label'] = "Active Configuration";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=config-info";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Developers Only";//probably will not be used
		$icons[] = $icon;
			$icon['label'] = "System Variables";
			$icon['link'] = $Q{'base_url'}."/?module={$modref}&task=sys-vars";
			$icon['icon'] = $modImageSrc;
			$icon['group'] = "Developers Only";//probably will not be used
		$icons[] = $icon;
	return $icons;
}

?>
